/**
 * 2009-10-26 下午03:14:01 
 * esignature
 * com.smc.servlet.filter
 * LoginFilter
 */
package com.smc.servlet.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.smc.security.Authentication;
import com.smc.security.AuthenticationManager;
import com.smc.security.SecurityContextHolder;

/**
 * @author chenheng
 * @version 0.0.1
 * 登陆信息过滤器
 */
public class LoginFilter implements Filter {
	/**
	 * 过滤链配制信息
	 */
	private FilterConfig config ;
	/**
	 * 销毁方法
	 */
	public void destroy() {

	}
	/**
	 * 登陆信息过滤
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain ) throws IOException, ServletException {
		HttpServletRequest httpRequest = (HttpServletRequest) request; 
		HttpServletResponse httpResponse = (HttpServletResponse) response ;
		String requiredUrl = httpRequest.getRequestURL().toString();
		String resourceUrl = requiredUrl.substring(requiredUrl.lastIndexOf('/'));
		if (resourceUrl.equals("/")) {
			httpRequest.getRequestDispatcher("/index.jsp").forward(request, response);
		}else if (resourceUrl.equals("/toLogin.shtml") || resourceUrl.equals("/login.shtml") || resourceUrl.equals("/toRegister.shtml") || resourceUrl.equals("/toNoHospitalRegister.shtml") || resourceUrl.equals("/register.shtml") ) {
			chain.doFilter(request, response);
		} else {
			HttpSession session = httpRequest.getSession();
			Authentication auth = SecurityContextHolder
					.getAuthentication(session);
			if (auth != null && auth.isAuthenticated()) {
				chain.doFilter(request, response);
			} else {
				// doLogin(httpRequest,httpResponse);
				// if ( !
				// SecurityContextHolder.getAuthentication(httpRequest.getSession()).isAuthenticated())
				// {
				// config.getServletContext().getRequestDispatcher("/login.jsp").forward(request,
				// response);
				httpResponse.sendRedirect(httpRequest.getContextPath() + "/toLogin.shtml");
				//httpRequest.getRequestDispatcher("/login.jsp").forward(request, response);
				//}
			}
		}
	}
	/**
	 * 初始化方法
	 */
	public void init(FilterConfig config) throws ServletException {
		this.config = config ;
	}
	/**
	 * 取得WEB应用上下文
	 * @param config	过滤链配制
	 * @return
	 */
	public ApplicationContext getContext(FilterConfig config) {
		return WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext());
	}
	/**
	 * 登陆
	 * @param request
	 * @param response
	 * @throws ServletException
	 */
	public synchronized void doLogin(HttpServletRequest request,HttpServletResponse response) throws ServletException {
		String authClass = config.getInitParameter("authClass");
		ApplicationContext appContext = getContext(config);
		AuthenticationManager authManager = (AuthenticationManager) appContext.getBean(authClass); ;
		authManager.remembermeAuthentication(request, response);
	}

}
